#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct treenode {
    int data;
    struct treenode* left;
    struct treenode* right;
};
typedef struct treenode treenode;
struct treenode* troot;
treenode* build() {
    int a;
    scanf("%d", &a);
    if (a == 0) return NULL;
    treenode* root = (treenode*)malloc(sizeof(treenode));
    root->data = a;
    root->left = build();
    root->right = build();
    return root;
}

treenode* find(treenode* root, int k) {
    if (root == NULL) return NULL;
    if (root->data == k) return root;
   treenode* L= find(root->left);
   if (L != NULL) return L;
   treenode* F= find(root->right);
   return F;
}

treenode* findfather(treenode* root, treenode* child) {
    if (root == NULL || root == child) return NULL;
    if (root->left == child || root->right == child) return root;
    treenode* ans = findfather(root->left, child);
    if (ans != NULL) return ans;
    return findfather(root->right, child);
}

int main() {
    troot = build();
    int m,i;
    int k[110];
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
        scanf("%d", &k[i]);
    };
   
    for (i = 0; i < m; i++) {
        treenode* child= find(troot, k[i]);
        treenode* value = findfather(troot, child);
        printf("%d\n", value->data);
    };
    return 0;
}